package com.zerdoor.util;

import java.io.File;
import java.security.CodeSource;
import java.security.ProtectionDomain;

/**
 * Created by xiaobo on 2014/11/10.
 */
public class Path {

    /**项目路径路径*/
    protected static final String PATH;

    static
    {
        // 类的本地路径
        String local = null;
        try
        {
            Class c = Path.class;
            ProtectionDomain pd = c.getProtectionDomain();
            CodeSource cs = pd.getCodeSource();
            local = cs.getLocation().toString();
        } catch (Exception e)
        {
            throw new ExceptionInInitializerError(e);
        }

        if (local == null)
        {
            throw new ExceptionInInitializerError("查找类WaterMark的本地路径失败");
        }

        String protocol = "file:/";
        if (local.startsWith(protocol))
        {
            local = local.substring(protocol.length());
        }

        String webInfPath0 = "/WEB-INF/lib/";
        String webInfPath1 = "/WEB-INF/classes/";
        int index = 0;
        if ((index = local.indexOf(webInfPath0)) != -1
                || (index = local.indexOf(webInfPath1)) != -1)
        {
            local = local.substring(0, index);
        } else
        {
            String message = "查找应用程序的根路径失败。";
            throw new ExceptionInInitializerError(message);
        }
        PATH = "/" + local + File.separator;

    }
}
